package com.Gwo.org.controller;

import com.Gwo.basic.controller.BaseController;
import com.Gwo.basic.query.BaseQuery;
import com.Gwo.basic.utils.PageList;
import com.Gwo.basic.utils.Result;
import com.Gwo.org.domain.Department;
import com.Gwo.org.domain.Product;
import com.Gwo.org.domain.ProductType;
import com.Gwo.org.query.ProductQuery;
import com.Gwo.org.query.ProductTypeQuery;
import com.Gwo.org.service.IProductService;
import com.Gwo.org.service.IProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/protype")
public class ProductTypeController{

    @Autowired
    private IProductTypeService service;
    @Autowired
    private IProductService productService;

    @RequestMapping("/list")
    @ResponseBody
    public List<ProductType> findAll(){
        return service.findAll();
    }

    @PostMapping
    public PageList<ProductType> queryByPage(@RequestBody ProductTypeQuery query){
        PageList<ProductType> list = service.queryByPage(query);
        return list;
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            productService.deleteByTypeId(id);
            return Result.me().success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.me().fail();
        }
    }

    @PutMapping
    public Result addOrUpdate(@RequestBody ProductType productType){
        if(productType.getId()==null){
            service.add(productType);
            return Result.me().success();
        }else {
            service.update(productType);
            return Result.me().success();
        }
    }

    @PatchMapping
    public Result batchDelete(@RequestBody List<Long> ids){
        try{
            service.batchDelete(ids);
            return Result.me().success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    @GetMapping("/{id}")
    public List<Product>findProducts(@PathVariable("id") Long pid){
        List<Product> list = productService.anotherfindById(pid);
        list.forEach(System.out::println);
        return list;
    }
}
